package com.example.airquality.air.controller;


import com.example.airquality.air.entity.District;
import com.example.airquality.air.entity.QualityIndex;
import com.example.airquality.air.service.IDistrictService;
import com.example.airquality.air.service.IQualityIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 韩志龙
 * @version 1.0
 * @description: TODO
 * @date 2021/5/19 22:52
 */
@Controller
@RequestMapping("/add")
public class AddController {
    @Autowired
    private IDistrictService iDistrictService;
    @Autowired
    private IQualityIndexService iQualityIndexService;

    @RequestMapping("/toAddQuality")
    public String toAddQuality(Model model,Integer districtId){
        List<District> districtList = iDistrictService.list();
        model.addAttribute("districtList",districtList);
        return "add";
    }
    @RequestMapping("/addQualityIndex")
    @Transactional
    public String addQualityIndex(QualityIndex qualityIndex,Model model,HttpSession session){
        if(null != qualityIndex){
            qualityIndex.setLastModifyTime(new Date());
            boolean save = iQualityIndexService.save(qualityIndex);
            if(save){
                return "redirect:/qualityIndex/toFindAll";
            }
        }
        model.addAttribute("error_info","保存失败");
        return "redirect:add";

    }
}
